/// 网络套接字描述符
pub type Socket = i32;

///
pub const AF_INET: i32 = libc::AF_INET;
///
pub const SOCK_STREAM: i32 = libc::SOCK_STREAM;
///
pub const SOCK_DGRAM: i32 = libc::SOCK_DGRAM;
/// TCP协议码
pub const IPPROTO_TCP: i32 = libc::IPPROTO_TCP;
/// UDP协议码
pub const IPPROTO_UDP: i32 = libc::IPPROTO_UDP;
/// 套接字错误
pub const SOCKET_ERROR: i32 = -1;
/// 表示一个无效的套接字
pub const INVALID_SOCKET: Socket = -1;

/// 创建网络套接字
#[inline(always)]
pub fn socket(domain: i32, ty: i32, protocol: i32) -> Socket
{
    unsafe { libc::socket(domain, ty, protocol) }
}
/// 关闭网络套接字
#[inline(always)]
pub fn close(socket: Socket) -> i32
{
    unsafe { libc::close(socket) }
}
/// 绑定端口
#[inline(always)]
pub fn bind(socket: Socket, sockaddr: *const SockAddr) -> i32
{
    unsafe { libc::bind(socket, sockaddr as *const libc::sockaddr, std::mem::size_of::<libc::sockaddr>() as libc::socklen_t) }
}
/// 开始监听
#[inline(always)]
pub fn listen(socket: Socket, backlog: i32) -> i32
{
    unsafe { libc::listen(socket, backlog) }
}
/// 接受网络连接
#[inline(always)]
pub fn accept(socket: Socket, sockaddr: *mut SockAddr) -> Socket
{
    unsafe {
        let mut addrlen: u32 = std::mem::size_of::<SockAddr>() as u32;
        libc::accept(socket, sockaddr as *mut libc::sockaddr, &mut addrlen)
    }
}
/// 连接到网络
#[inline(always)]
pub fn connect(socket: Socket, sockaddr: *const SockAddr) -> i32
{
    unsafe { libc::connect(socket, sockaddr as *const libc::sockaddr, std::mem::size_of::<libc::sockaddr>() as u32) }
}
/// 接收网络数据
#[inline(always)]
pub fn recv(socket: Socket, buf: *mut u8, len: usize) -> isize
{
    unsafe { libc::recv(socket, buf as *mut libc::c_void, len, 0) }
}
/// 发送网络数据
#[inline(always)]
pub fn send(socket: Socket, buf: *const u8, len: usize) -> isize
{
    unsafe { libc::send(socket, buf as *const libc::c_void, len, 0) }
}

include!("select.rs");